#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#define width 640
#define height 480

	int ball_x,ball_y;
	int ball_vx ,ball_vy;
	int r ; 
	char c;
    int bar_x,bar_y;
	int bar_l,bar_r,bar_t,bar_b;

	void init(){
		ball_x = width/2;
    	ball_y = height/2;
		ball_vx = 1;
	    ball_vy = 1;
		r =20;
		bar_x =width/2;
        bar_l = bar_x -100;
    	bar_r = bar_x +100;
        bar_t = 450 ;
    	bar_b =480;
	}
	void show(){
    	setcolor(YELLOW);
		setfillcolor(WHITE);
		fillcircle(ball_x,ball_y,r);
		fillrectangle(bar_l,bar_t,bar_r,bar_b);

	}
	void autom(){
	 ball_x+= ball_vx;
     ball_y+= ball_vy;
	 if(ball_x<=r || ball_x>=width-r){
	    ball_vx = -ball_vx;
	 }
      if(ball_y<=r || ball_y>=height-r){
	    ball_vy = -ball_vy;
	  }
	  if(ball_x>=bar_l&&ball_x<=bar_r&&ball_y+30+r>=height){
	     ball_vy = -ball_vy;
	  }
	   bar_l = bar_x -100;
	   bar_r = bar_x +100;
	}
	  void clear(){
		  setcolor(BLACK);
		  setfillcolor(BLACK);
          fillcircle(ball_x,ball_y,r);

	  }
	  void move(){
	  
	   if(_kbhit()){
	   c = _getch();
	   switch(c){
	   case 'a' :
		   if(bar_l<=0)
			   bar_l = 0;
		   else
		   bar_x-=10;
		   break;
	   case 'd':
		   if(bar_l>=640)
		   bar_r = 640;
		   else
		   bar_x+= 10;
		   break;
	   }
	   }
	  }
	  
	   void main(){
	     initgraph(width,height);
	     init();
	     BeginBatchDraw();
		 while(1){
		 clear();
         fillrectangle(bar_l,bar_t,bar_r,bar_b);
		 autom();
		 move();
         clear();
		 show();
		  
		 //autom();
		 Sleep(3);
	     FlushBatchDraw();
		 }
	    EndBatchDraw();
	   }
	   

	   

